package chances.epg.api.tools;

import org.apache.commons.lang.StringUtils;

import chances.epg.commons.utils.JSONUtils;
import chances.epg.data.service.BaseDataRepertory;
import chances.epg.search.analyzer.SplitAnalyzerUtils;

public class ContentTool {
	private static final String SEPERATOR = "|";
	
    private static ContentTool instance;
	
	public static ContentTool getInstance() {
		if(instance == null) {
			instance = new ContentTool();
		}
		
		return instance;
	}
	
	/**
	 * 类型名称
	 * @param type
	 * @return
	 */
	public String typeName(String type) {
		if(StringUtils.isBlank(type)){
			return "";
		}else{
		    return BaseDataRepertory.getInstance().getTypeName(type);
		}
	}
	
	/**
	 * 运营标签
	 * @param opTags
	 * @return
	 */
	public String opTagNames(String opTags){
		if(StringUtils.isBlank(opTags)){
			return "";
		}
		
		String[] codes = StringUtils.split(opTags, SEPERATOR);
		String result = "";
		for (int i = 0; i < codes.length; i++) {
			String name = BaseDataRepertory.getInstance().getOpTagName(codes[i]);
			if(StringUtils.isNotBlank(name)){
				result += name;
				result += SEPERATOR;
			}
		}
		
		if(StringUtils.isNotBlank(result)){
			result = StringUtils.removeEnd(result, SEPERATOR);
		}
		
		return result;
	}
	
	public String baseTagNames(String baseTags){
		if(StringUtils.isBlank(baseTags)){
			return "";
		}
		
		String[] codes = StringUtils.split(baseTags, SEPERATOR);
		String result = "";
		for (int i = 0; i < codes.length; i++) {
			String name = BaseDataRepertory.getInstance().getBaseTagName(codes[i]);
			if(StringUtils.isNotBlank(name)){
				result += name;
				result += SEPERATOR;
			}
		}
		
		if(StringUtils.isNotBlank(result)){
			result = StringUtils.removeEnd(result, SEPERATOR);
		}
		
		return result;
	}
	
	public String personJson(String strPersons) {
		String[] persons = SplitAnalyzerUtils.getInstance().split(strPersons);
		return JSONUtils.fromObject(persons);
	}
	
}
